package com.gongpingjialoan.adapter;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.pay.RefundActivity;
import com.gongpingjialoan.bean.Monitor;
import com.gongpingjialoan.bean.car.OrderRecord;
import com.gongpingjialoan.utility.StepMonitor;

import java.util.List;

/**
 * Created by Administrator on 2016/5/19.
 */
public class OrderAdapter extends BaseAdapter {

    private List<OrderRecord.DataBean.RecordsBean> mOrderRecord;

    private Activity mContext;

    private LayoutInflater mLayoutInflater;

    public OrderAdapter(List<OrderRecord.DataBean.RecordsBean> orderRecord, Activity context) {
        mOrderRecord = orderRecord;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<OrderRecord.DataBean.RecordsBean> mOrderRecord) {
        this.mOrderRecord = mOrderRecord;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mOrderRecord.size();
    }

    @Override
    public Object getItem(int position) {
        return mOrderRecord.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.order_item, null);
            holder = new ViewHolder();
            holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.pic);
            holder.tvCarName = (TextView) convertView.findViewById(R.id.carName);
            holder.tvCarTitle = (TextView) convertView.findViewById(R.id.title);
            holder.mileTextView = (TextView) convertView.findViewById(R.id.mile);
            holder.priceTextView = (TextView) convertView.findViewById(R.id.price);
            holder.refundTv = (TextView) convertView.findViewById(R.id.refund_tv);
            holder. bargainSource = (TextView) convertView.findViewById(R.id.bargain_source);
            holder.phoneLl = (LinearLayout)convertView.findViewById(R.id.phone_ll);
            holder.personTv = (TextView) convertView.findViewById(R.id.person_tv);
            holder.renzhengImageView = (ImageView) convertView.findViewById(R.id.renzheng);
            holder.yanTextView = (TextView) convertView.findViewById(R.id.yan);
            holder.zhibaoImageView = (ImageView) convertView.findViewById(R.id.zhibao_view);
            holder.typeTextView = (TextView) convertView.findViewById(R.id.type);
            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.tag_first);
        }
        final OrderRecord.DataBean.RecordsBean source = mOrderRecord.get(position);
        convertView.setTag(R.id.tag_second, source);
        final OrderRecord.DataBean.RecordsBean.CarBean carBean = source.getCar();
        if(source != null && carBean != null){
            if(source.isCan_refund()){
                holder.refundTv.setTextColor(mContext.getResources().getColor(R.color.discount_title));
                holder.refundTv.setText("申请退款 >");
                holder.refundTv.setClickable(true);
                holder.refundTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        StepMonitor.getInstance().addMonitor(new Monitor("click", "my_orderList_applyRefund", System.currentTimeMillis(), 1f));
                        Intent intent = new Intent(mContext, RefundActivity.class);
                        intent.putExtra("car_id",source.getId());
                        intent.putExtra("type", source.getType());
                        mContext.startActivityForResult(intent, 0);
                    }
                });
            }else{
                holder.refundTv.setClickable(false);
                holder.refundTv.setText(source.getStatus_zh());
                holder.refundTv.setTextColor(mContext.getResources().getColor(R.color.text_grey_dark));

            }
            if ("owner".equals(source.getContact_type())){
                holder.personTv.setText("车主");
            }else{
                holder.personTv.setText("客服");
            }
            holder.phoneLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String phone;
                    if ("owner".equals(source.getContact_type())){
                        phone = carBean.getPhone();
                        StepMonitor.getInstance().addMonitor(new Monitor("click", "my_orderList_contactDealer", System.currentTimeMillis(), 1f));
                    }else{
                        phone = "4000253500";
                        StepMonitor.getInstance().addMonitor(new Monitor("click", "my_orderList_staff", System.currentTimeMillis(), 1f));
                    }
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
                    mContext.startActivity(intent);
                }
            });
            if (carBean.isIs_certify()) {
                if ("4s".equals(carBean.getDealer_category())) {
                    holder.renzhengImageView.setImageResource(R.drawable.renzheng_4s);
                } else {
                    holder.renzhengImageView.setImageResource(R.drawable.renzheng);
                }
                holder.renzhengImageView.setVisibility(View.VISIBLE);
            } else {
                holder.renzhengImageView.setVisibility(View.GONE);
            }

            if(carBean.isCs_clean()){
                holder.yanTextView.setVisibility(View.VISIBLE);
            }else{
                holder.yanTextView.setVisibility(View.GONE);
            }

            if (carBean.getQs_tags().contains("质保")) {
                holder.zhibaoImageView.setVisibility(View.VISIBLE);
                holder.typeTextView.setVisibility(View.GONE);
            } else {
                holder.zhibaoImageView.setVisibility(View.GONE);
                holder.typeTextView.setVisibility(View.VISIBLE);
                if ("personal".equals(carBean.getSource_type())) {
                    holder.typeTextView.setText("个");
                    holder.typeTextView.setBackgroundResource(R.drawable.sort_ge);
                    holder.typeTextView.setTextColor(mContext.getResources().getColor(R.color.white));
                } else if("dealer".equals(carBean.getSource_type()) || "cpo".equals(carBean.getSource_type())|| "odealer".equals(carBean.getSource_type())){
                    holder.typeTextView.setText("商");
                    holder.typeTextView.setBackgroundResource(R.drawable.sort_shang);
                    holder.typeTextView.setTextColor(mContext.getResources().getColor(R.color.white));
                }else{
                    holder.typeTextView.setVisibility(View.GONE);
                }
            }
            holder.tvCarName.setText(carBean.getModel_slug_zh());
            if ("".equals(carBean.getModel_detail_slug_zh())) {
                holder.tvCarTitle.setText(carBean.getTitle());
            } else {
                holder.tvCarTitle.setText(carBean.getModel_detail_slug_zh());
            }
            holder.tvCarTitle.setText(carBean.getModel_detail_slug_zh());
            if("car_ordered_record".equals(source.getType())){
                holder.priceTextView.setText(carBean.getPrice());
            }else{
                holder.priceTextView.setText(source.getPromo_car().getPromo_priceX()+"");
            }

            holder.mileTextView.setText( carBean.getMile() + "万公里/" + carBean.getYear() + "年上牌/"  +carBean.getCity());
            Glide.with(mContext).load(carBean.getThumbnail()).placeholder(R.drawable.car_loading).centerCrop().crossFade().into(holder.imgThumbnail);
        }
        return convertView;
    }


    public static class ViewHolder {
        public ImageView imgThumbnail;

        public TextView tvCarName;

        public TextView tvCarTitle;

        public TextView mileTextView;

        public TextView priceTextView;

        public TextView refundTv;

        public  TextView bargainSource;

        public  TextView personTv;

        public LinearLayout phoneLl;

        public ImageView renzhengImageView;

        public TextView yanTextView;

        public TextView typeTextView;

        public ImageView zhibaoImageView;


    }
}

